﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 经典2048
{
    public partial class MainForm : Form
    {
        private Label[,] labels = new Label[4, 4];
        private Game game = new Game();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Init();
            Draw();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    if (labels[i, j] == null)
                    {
                        labels[i, j] = new Label();
                    }
                    Label label = labels[i, j];
                    label.Text = "";
                    label.TextAlign = ContentAlignment.MiddleCenter;
                    label.Font = new Font("times new roman", 28f);
                    label.AutoSize = false;
                    label.Size = new Size(100, 100);
                    label.Location = new Point(j * label.Size.Width, i * label.Size.Height);
                    label.BorderStyle = BorderStyle.FixedSingle;
                    label.BackColor = Color.Gray;
                    this.Controls.Add(label);
                }
            }

            this.game.Init();
        }

        /// <summary>
        /// 游戏中数据渲染到标签数组
        /// </summary>
        private void Draw()
        {
            Dictionary<int, Color> colors = new Dictionary<int, Color>();
            colors.Add(0, Color.Gray);
            colors.Add(2, Color.Red);
            colors.Add(4, Color.Orange);
            colors.Add(8, Color.Yellow);
            colors.Add(16, Color.Green);
            colors.Add(32, Color.Cyan);
            colors.Add(64, Color.Blue);
            colors.Add(128, Color.Purple);
            colors.Add(256, Color.Pink);
            colors.Add(512, Color.AliceBlue);
            colors.Add(1024, Color.Brown);
            colors.Add(2048, Color.White);

            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Label label = labels[i, j];
                    label.BackColor = colors[game.map[i, j]];
                    label.Text = game.map[i, j].ToString();
                    if (game.map[i, j] == 0)
                    {
                        label.Text = "";
                    }
                }
            }
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            //请求重开
            if (e.KeyCode == Keys.O)
            {
                if (MessageBox.Show("确定重新开始吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
                {
                    return;
                }

                Init();
                Draw();
                return;
            }

            //请求移动或合并
            switch (e.KeyCode)
            {
                case Keys.W:
                case Keys.Up:
                    this.game.Turn(TurnType.上);
                    break;
                case Keys.A:
                case Keys.Left:
                    this.game.Turn(TurnType.左);
                    break;
                case Keys.S:
                case Keys.Down:
                    this.game.Turn(TurnType.下);
                    break;
                case Keys.D:
                case Keys.Right:
                    this.game.Turn(TurnType.右);
                    break;
                default:
                    return;
            }

            Draw();
            if (this.game.IsOver())
            {
                Thread.Sleep(200);
                if (this.game.Success())
                {
                    MessageBox.Show("已达成目标，请按O键重新开始", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("你输掉游戏，请按O键重新开始", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
